package cn.tj212.inmall.service.impl;

import cn.tj212.inmall.common.ServerResponse;
import cn.tj212.inmall.dao.CategoryMapper;
import cn.tj212.inmall.pojo.Category;
import cn.tj212.inmall.service.ICategoryService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;


@Service("iCategoryService")
public class CategoryServiceImpl implements ICategoryService {

    private Logger logger= LoggerFactory.getLogger(CategoryServiceImpl.class);

    @Autowired
    private CategoryMapper categoryMapper;

    public ServerResponse addCategory(String categoryName,Integer parentId){
        if (parentId==null||StringUtils.isBlank(categoryName)){
            return ServerResponse.createByErrorMsg("param wrong");
        }
        Category category=new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
        category.setStatus(true);

        int rowCount=categoryMapper.insert(category);
        if (rowCount>0){
            return ServerResponse.createBySuccessMsg("Add new category success");
        }
        return ServerResponse.createByErrorMsg("Add new category failed");
    }

    public ServerResponse updateCategoryName(Integer categoryId,String categoryName){
        if (categoryId==null){
            return ServerResponse.createByErrorMsg("CategoryId can't be null");
        }
        Category category=new Category();
        category.setName(categoryName);
        category.setId(categoryId);

        int rowCount=categoryMapper.updateByPrimaryKeySelective(category);
        if (rowCount>0){
            return ServerResponse.createBySuccessMsg("Update category success!");
        }else {
            return ServerResponse.createByErrorMsg("Update category failed!");
        }
    }

    public ServerResponse<List<Category>> getChildrenParallelCategory(Integer categoryId){
        List<Category> categoryList=categoryMapper.selectCategoryChildrenByParentId(categoryId);
        if (CollectionUtils.isEmpty(categoryList)){
            logger.info("Can't find subCategory");
        }
        return ServerResponse.createBySuccess(categoryList);
    }

    @Override
    public ServerResponse<List<Category>> selectCategoryAndChildrenById(Integer categoryId) {
        return null;
    }
}
